package com.vanke.vms.management.dao.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.vanke.vms.common.domain.AddCtrl;
import com.vanke.vms.common.domain.AddInfo;
import com.vanke.vms.common.domain.Info;
import com.vanke.vms.management.dao.ModifyInfoDao;

public class ModifyInfoDaoImpl extends HibernateDaoSupport implements ModifyInfoDao{
	public Info loadInfo(String modifyInfoId){
		List<Info> infoList = (List<Info>)getHibernateTemplate().find(
				"from Info as inf " + " where inf.infoId = " +  modifyInfoId);
		if (infoList.size() != 0)
			return infoList.get(0);
		else
			return null;
	}
	
	public List<AddInfo> loadAddiInfoList(String modifyInfoId){
		List<AddCtrl> addCtrlList = (List<AddCtrl>)getHibernateTemplate().find("from AddCtrl as adc order by adc.pname");
		List<AddInfo> addInfoList = (List<AddInfo>)getHibernateTemplate().find(
				"from AddInfo as addInf " + " where addInf.infoId = " +  modifyInfoId);
		List<AddInfo> allAddInfoList = new ArrayList<AddInfo>();
		for(int i=0;i<addCtrlList.size();i++){
			AddInfo tmp = new AddInfo();
			tmp.setPname(addCtrlList.get(i).getPname());
			tmp.setCname(addCtrlList.get(i).getCname());
			allAddInfoList.add(tmp);
		}
		for(int i=0;i<allAddInfoList.size();i++){
			for(int j=0;j<addInfoList.size();j++){
				if(allAddInfoList.get(i).getPname().equals(addInfoList.get(j).getPname()) &&
						allAddInfoList.get(i).getCname().equals(addInfoList.get(j).getCname())){
					AddInfo tmp = new AddInfo();
					tmp.setPname(addInfoList.get(j).getPname());
					tmp.setCname(addInfoList.get(j).getCname());
					tmp.setContent(addInfoList.get(j).getContent());
					allAddInfoList.add(i,tmp);
					allAddInfoList.remove(i+1);
					break;
				}
			}
		}
			return allAddInfoList;
	}
	
	public String modifyInfo(String modifyInfoId, Info info, List<AddInfo> addiInfoList){
//		try{
			info.setInfoId(Integer.parseInt(modifyInfoId));	
			//Info tmpInfo = (Info)getHibernateTemplate().find("from Info where infoId="+modifyInfoId).get(0);
			//if(tmpInfo != info){
				getHibernateTemplate().update(info);
			//}
			for(int i=0;i<addiInfoList.size();i++){
				String hql = "from AddInfo as addi where addi.infoId=" + modifyInfoId+ " and addi.pname='" +
					addiInfoList.get(i).getPname()+"' and addi.cname='"+addiInfoList.get(i).getCname()+"'";
				AddInfo tmpaddinfo = addiInfoList.get(i);
				tmpaddinfo.setInfoId(Integer.parseInt(modifyInfoId));
				if(getHibernateTemplate().find(hql).size() == 0)
				{
					getHibernateTemplate().persist(tmpaddinfo);
				}
				else{//hql必须要以primary key才能用update()更新。。。
					SessionFactory sessionfactory = getHibernateTemplate()
						.getSessionFactory();
					Session session = sessionfactory.openSession();
					String sql = "update vms.addinfo set pname='"+tmpaddinfo.getPname()+
							"',cname='"+tmpaddinfo.getCname()+"',content='"+tmpaddinfo.getContent()+
							"' where infoid="+modifyInfoId+" and pname='"+tmpaddinfo.getPname()+
							"' and cname='"+tmpaddinfo.getCname()+"'";
					session.createSQLQuery(sql);
					session.close();
				}
			}
			return "success";
//		}
//		catch(Exception e){
//			return "error";
//		}
	}
}
